// 用户模块
import { userAccountLogin } from '@/api/user.js'

export default {
  namespaced: true,
  state: {
    uname: 'lisi',
    profile: {
      id: '',
      avatar: '',
      nickname: '',
      account: '',
      mobile: '',
      token: ''
    }
  },
  mutations: {
    updateUname(state, payload) {
      state.uname = payload
    },
    updateUserInfo(state, payload) {
      // 更新用户信息
      state.profile = payload
    }
  },
  actions: {
    // 基于账号的登录
    async accountLogin(context, payload) {
      // 调用接口实现登录
      try {
        const ret = await userAccountLogin(payload)
        if (ret.result) {
          // 登录成功
          const { id, avatar, nickname, account, mobile, token } = ret.result
          context.commit('updateUserInfo', {
            id,
            avatar,
            nickname,
            account,
            mobile,
            token
          })
          return true
        } else {
          // 登录失败
          return false
        }
      } catch {
        return false
      }
    }
  },
  getters: {}
}
